package pl.dsp.dsp1.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;
import javax.swing.DefaultComboBoxModel;

import pl.dsp.dsp1.processing.GeneratorFactory;
import pl.dsp.dsp1.processing.GeneratorFactory.Source;

import javax.swing.JList;

public class MainWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3486405218142866444L;
	private JSpinner aValue;
	private JSpinner t1Value;
	private JSpinner dValue;
	private JSpinner tValue;
	private JSpinner kwValue;
	private JSpinner frequencyValue;
	private JSpinner rangesValues;
	private JSpinner bitdepthValue;
	private JSpinner sincSamplesValue;
	private JComboBox signalValue;
	private JPanel graphContainer;
	private JList signalList = new JList();
	private WindowController controller = new WindowController(this);

	public int getSincSamples() {
		return (Integer) sincSamplesValue.getModel().getValue();
	}

	public int getBitdepth() {
		return (Integer) bitdepthValue.getModel().getValue();
	}

	public int getRanges() {
		return (Integer) rangesValues.getModel().getValue();
	}

	public double getA() {
		return (Double) aValue.getModel().getValue();
	}

	public double getT1() {
		return (Double) t1Value.getModel().getValue();
	}

	public double getD() {
		return (Double) dValue.getModel().getValue();
	}

	public double getT() {
		return (Double) tValue.getModel().getValue();
	}

	public double getKw() {
		return (Double) kwValue.getModel().getValue();
	}

	public double getFp() {
		return (Double) frequencyValue.getModel().getValue();
	}

	public GeneratorFactory.Source getSignalSource() {
		return (GeneratorFactory.Source) signalValue.getModel()
				.getSelectedItem();
	}

	public int[] getSelectedIndices() {
		return signalList.getSelectedIndices();
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setTitle("Signal processing suite");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(50, 30, 1155, 685);
		JPanel contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel bottomPanel = new JPanel(new BorderLayout(0, 2));
		bottomPanel.setBorder(new MatteBorder(2, 0, 0, 0, Color.LIGHT_GRAY));
		JPanel signalSettingsPanel = new JPanel(new GridLayout(0, 8));
		bottomPanel.add(signalSettingsPanel, BorderLayout.CENTER);
		signalSettingsPanel.setPreferredSize(new Dimension(0, 70));
		signalSettingsPanel.setBorder(new EmptyBorder(4, 4, 4, 4));
		contentPane.add(bottomPanel, BorderLayout.SOUTH);

		JLabel lblSignal = new JLabel("Signal:");
		signalSettingsPanel.add(lblSignal);

		signalValue = new JComboBox();
		signalValue.setModel(new DefaultComboBoxModel(Source.values()));
		signalSettingsPanel.add(signalValue);

		final JLabel lblAmplitude = new JLabel("A:");
		signalSettingsPanel.add(lblAmplitude);

		aValue = new JSpinner();
		aValue.setModel(new SpinnerNumberModel(new Double(1), null, null,
				new Double(1)));
		signalSettingsPanel.add(aValue);

		final JLabel lblT = new JLabel("t1 (s):");
		signalSettingsPanel.add(lblT);

		t1Value = new JSpinner();
		t1Value.setModel(new SpinnerNumberModel(new Double(0), null, null,
				new Double(1)));
		signalSettingsPanel.add(t1Value);

		final JLabel lblD = new JLabel("d (s):");
		signalSettingsPanel.add(lblD);

		dValue = new JSpinner();
		dValue.setModel(new SpinnerNumberModel(new Double(1), null, null,
				new Double(1)));
		signalSettingsPanel.add(dValue);

		final JLabel lblT_1 = new JLabel("T (s):");
		signalSettingsPanel.add(lblT_1);

		tValue = new JSpinner();
		tValue.setModel(new SpinnerNumberModel(new Double(1), null, null,
				new Double(1)));
		signalSettingsPanel.add(tValue);

		final JLabel lblKw = new JLabel("kw:");
		signalSettingsPanel.add(lblKw);

		kwValue = new JSpinner();
		kwValue.setModel(new SpinnerNumberModel(new Double(0), null, null,
				new Double(1)));
		signalSettingsPanel.add(kwValue);

		graphContainer = new JPanel();
		contentPane.add(graphContainer, BorderLayout.CENTER);

		JLabel lblNewLabel = new JLabel("Fp (Hz):");
		signalSettingsPanel.add(lblNewLabel);

		frequencyValue = new JSpinner();
		frequencyValue.setModel(new SpinnerNumberModel(new Double(100), null,
				null, new Double(1)));
		signalSettingsPanel.add(frequencyValue);

		JLabel lblNewLabel_1 = new JLabel("histogram ranges:");
		signalSettingsPanel.add(lblNewLabel_1);

		rangesValues = new JSpinner();
		rangesValues.setModel(new SpinnerNumberModel(new Integer(5), null,
				null, new Integer(1)));
		signalSettingsPanel.add(rangesValues);

		JLabel lblNewLabel_3 = new JLabel("bit depth:");
		signalSettingsPanel.add(lblNewLabel_3);

		bitdepthValue = new JSpinner();
		bitdepthValue.setModel(new SpinnerNumberModel(new Integer(5), null,
				null, new Integer(1)));
		signalSettingsPanel.add(bitdepthValue);

		JLabel lblNewLabel_4 = new JLabel("sinc samples:");
		signalSettingsPanel.add(lblNewLabel_4);

		sincSamplesValue = new JSpinner();
		sincSamplesValue.setModel(new SpinnerNumberModel(new Integer(5), null,
				null, new Integer(1)));
		signalSettingsPanel.add(sincSamplesValue);

		JButton previewButton = new JButton("preview");
		previewButton.addActionListener(controller);
		bottomPanel.add(previewButton, BorderLayout.SOUTH);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.EAST);
		panel.setLayout(new BorderLayout(0, 10));

		signalList.setBorder(new MatteBorder(0, 2, 0, 2, Color.LIGHT_GRAY));
		signalList.setPreferredSize(new Dimension(150, 0));
		panel.add(signalList, BorderLayout.WEST);
		signalList.setModel(controller.getSignalListModel());

		JPanel toolMenu = new JPanel();
		toolMenu.setBorder(new EmptyBorder(2, 3, 2, 3));
		toolMenu.setLayout(new GridLayout(0, 2, 0, 2));
		panel.add(toolMenu, BorderLayout.EAST);

		toolMenu.add(new JLabel("General:"));
		toolMenu.add(new JLabel());
		JButton plotButton = new JButton("plot");
		plotButton.addActionListener(controller);
		toolMenu.add(plotButton);

		JButton btnNewButton_1 = new JButton("push");
		toolMenu.add(btnNewButton_1);
		btnNewButton_1.addActionListener(controller);

		JButton removeButton = new JButton("remove");
		removeButton.addActionListener(controller);
		toolMenu.add(removeButton);

		JButton swapButton = new JButton("swap");
		swapButton.addActionListener(controller);
		toolMenu.add(swapButton);

		JButton renameButton = new JButton("rename");
		renameButton.addActionListener(controller);
		toolMenu.add(renameButton);

		toolMenu.add(new JLabel());
		toolMenu.add(new JLabel("File access:"));
		toolMenu.add(new JLabel());
		JButton saveButton = new JButton("save");
		saveButton.addActionListener(controller);
		toolMenu.add(saveButton);

		JButton loadButton = new JButton("load");

		loadButton.addActionListener(controller);
		toolMenu.add(loadButton);

		JButton saveJsonButton = new JButton("save json");
		saveJsonButton.addActionListener(controller);
		toolMenu.add(saveJsonButton);

		JButton btnLoadJson = new JButton("load json");
		btnLoadJson.addActionListener(controller);
		toolMenu.add(btnLoadJson);

		toolMenu.add(new JLabel("Functional:"));
		toolMenu.add(new JLabel());
		JButton histogramButton = new JButton("histogram");
		histogramButton.addActionListener(controller);
		toolMenu.add(histogramButton);

		JButton magnphaseButton = new JButton("mag&phase");
		magnphaseButton.addActionListener(controller);
		toolMenu.add(magnphaseButton);

		JButton quantizeButton = new JButton("quantize");
		quantizeButton.addActionListener(controller);
		toolMenu.add(quantizeButton);

		JButton firstOrderInterpolationButton = new JButton("1st interpolation");
		firstOrderInterpolationButton.addActionListener(controller);
		toolMenu.add(firstOrderInterpolationButton);

		JButton sincInterpolationButton = new JButton("sinc interpolation");
		sincInterpolationButton.addActionListener(controller);
		toolMenu.add(sincInterpolationButton);

		JButton convolutionButton = new JButton("convolution");
		convolutionButton.addActionListener(controller);
		toolMenu.add(convolutionButton);

		JButton correlationButton = new JButton("correlation");
		correlationButton.addActionListener(controller);
		toolMenu.add(correlationButton);

		toolMenu.add(new JLabel());
		toolMenu.add(new JLabel("Params/Errors:"));
		toolMenu.add(new JLabel());

		JButton paramsButton = new JButton("params");
		paramsButton.addActionListener(controller);
		toolMenu.add(paramsButton);

		JButton errorsButton = new JButton("errors");
		errorsButton.addActionListener(controller);
		toolMenu.add(errorsButton);

		toolMenu.add(new JLabel("Operations:"));
		toolMenu.add(new JLabel());

		JButton addButton = new JButton("add");
		addButton.addActionListener(controller);
		toolMenu.add(addButton);

		JButton substractButton = new JButton("substract");
		substractButton.addActionListener(controller);
		toolMenu.add(substractButton);

		JButton multiplyButton = new JButton("multiply");
		multiplyButton.addActionListener(controller);
		toolMenu.add(multiplyButton);

		JButton divideButton = new JButton("divide");
		divideButton.addActionListener(controller);
		toolMenu.add(divideButton);

		toolMenu.add(new JLabel("Windows:"));
		toolMenu.add(new JLabel());

		final FilterWindow filterWindow = new FilterWindow(this, controller);
		final RadarWindow radarWindow = new RadarWindow(this, controller);
		final TransformationsWindow transformationsWindow = new TransformationsWindow(this, controller);

		JButton filteringButton = new JButton("filtering");
		filteringButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				filterWindow.setVisible(true);
			}
		});
		toolMenu.add(filteringButton);

		JButton radarButton = new JButton("radar");
		radarButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				radarWindow.setVisible(true);
			}
		});
		toolMenu.add(radarButton);
		
		JButton transformationsButton = new JButton("transformations");
		transformationsButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				transformationsWindow.setVisible(true);
			}
		});
		toolMenu.add(transformationsButton);
	}

	protected JPanel getGraphContainer() {
		return graphContainer;
	}
}
